package cn.zhangjie.user.service.impl;

import cn.zhangjie.basic.constant.RegisterConstant;
import cn.zhangjie.basic.exception.BusinssException;
import cn.zhangjie.basic.service.imp.BasicServiceImpl;
import cn.zhangjie.basic.util.Md5Utils;
import cn.zhangjie.basic.util.StrUtils;
import cn.zhangjie.user.domain.Logininfo;
import cn.zhangjie.user.domain.User;
import cn.zhangjie.user.dto.RegisterDto;
import cn.zhangjie.user.mapper.LogininfoMapper;
import cn.zhangjie.user.mapper.UserMapper;
import cn.zhangjie.user.service.IUserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.swing.*;

@Service
public class UserServiceImpl extends BasicServiceImpl<User> implements IUserService {
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private LogininfoMapper LogininfoMapper;

    @Override
    public User findByphone(String phone) {
        User user = userMapper.findByphone(phone);
        return user;
    }

    @Override
    public void phoneRegister(RegisterDto dto) {
        //1校验，非空校验
        String phone = dto.getPhone();
        String verifyCode = dto.getVerifyCode();
        String password = dto.getPassword();
        String comfirmPassword = dto.getComfirmPassword();
        if(StringUtils.isEmpty(phone)||
           StringUtils.isEmpty(verifyCode)||
           StringUtils.isEmpty(password)||
           StringUtils.isEmpty(comfirmPassword)
        ){
            throw new BusinssException("参数不能为空");
        }
        //2两次密码是否一致
        if(!password.equals(comfirmPassword)){
            throw new BusinssException("两次密码不一致，请重新输入!");
        }
        //3手机号校验，是否被注册
        User userphone = userMapper.findByphone(phone);
        if(userphone!=null){
            throw new BusinssException("手机已经被注册，请直接登录");
        }
        //4验证码是否过期
        Object obj = redisTemplate.opsForValue().get(phone + RegisterConstant.PHONEREGISTER);
        if(obj==null){
            throw new BusinssException("验证码已经过期，请重新获取");
        }
        //5校验验证码是否正确
        String redisCode = obj.toString().split(":")[0];
        if(!verifyCode.equalsIgnoreCase(redisCode)){
            throw new BusinssException("验证码输入错误，请重新输入");
        }
        //6保存数据，user， logininfo
        //先去获取logininfo对象获取user对象
        Logininfo logininfo = dto2logininfo(dto);
        //先保存logininfo表，返回自增主键
        User user = logininfo2User(logininfo);
        //先保存logininfo表，返回自增主键
        LogininfoMapper.save(logininfo);
        //把自增主键赋值给user对象，在保存user
        user.setLogininfo_id(logininfo.getId());
        userMapper.save(user);

    }



    private  User logininfo2User(Logininfo logininfo){
        User user = new User();
        //copy相同的属性
        BeanUtils.copyProperties(logininfo,user);
        return user;
    }

    /**
     * 创建logininfo对象
     * @param dto
     * @return
     */
    public Logininfo  dto2logininfo(RegisterDto dto){
        Logininfo logininfo = new Logininfo();
        logininfo.setUsername(dto.getPhone());
        logininfo.setPhone(dto.getPhone());
        logininfo.setType(1);
        //生成盐值
        String salt = StrUtils.getComplexRandomString(32);
        //通过和密码进行加密  ，加盐加密
        String dbPassword = Md5Utils.encrypByMd5(salt + dto.getPassword());
        logininfo.setSalt(salt);
        logininfo.setPassword(dbPassword);
        return  logininfo;
    }




}
